package com.heima.article.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HotArticleServiceImpl implements HotArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private CacheService cacheService;

    @Override
    public void computeHotArticle() {
        //查询五天内的文章
        LocalDateTime date = LocalDateTime.now().minusDays(5);
        List<ApArticle> articleListByLast5days = apArticleMapper.findArticleListByLast5days(date);
        if (CollectionUtils.isEmpty(articleListByLast5days)){
            log.info("近五天没有跟新的文章");
            return;
        }
        //计算文章热度
        List<HotArticleVo> compute = compute(articleListByLast5days);
        //分组
        List<List<HotArticleVo>> channelId = CollectionUtil.groupByField(compute, "channelId");
        if (CollectionUtils.isEmpty(channelId)){
            log.warn("分组数据为空");
            return;
        }
        //根据分组进行排序
        //写入缓存
        for (List<HotArticleVo> hotArticleVos : channelId) {
            if (CollectionUtils.isEmpty(hotArticleVos)){
                      continue;
            }
            Integer channelId1 = hotArticleVos.get(0).getChannelId();
            sortAndCaching(hotArticleVos,channelId1.toString());
        }

        //推荐频道计算和处理
        sortAndCaching(compute,ArticleConstants.DEFAULT_TAG);
    }

    private void sortAndCaching(List<HotArticleVo> voList ,String channelId){
        //排序 stream sort
        voList.stream().sorted(new Comparator<HotArticleVo>() {
            @Override
            public int compare(HotArticleVo o1, HotArticleVo o2) {
                Integer score = o1.getScore();
                Integer score1 = o2.getScore();

                return score - score1 ;
            }
        }).collect(Collectors.toList());

        //截取30条
        if (voList.size()>30){
            voList=voList.subList(0,30);
        }
        Integer channelId1 = voList.get(0).getChannelId();
        //写入缓存
        cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE+channelId1, JSON.toJSONString(voList));
    }

    private List<HotArticleVo> compute(List<ApArticle> list){
        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        ArrayList<HotArticleVo> objects = new ArrayList<>();
        for (ApArticle apArticle : list) {
            if (apArticle==null){
                continue;
            }
            Integer score = 0;
            //阅读
            Integer views = apArticle.getViews();
            //点赞
            Integer likes = apArticle.getLikes();
            //评论
            Integer comment = apArticle.getComment();
            //收藏
            Integer collection = apArticle.getCollection();

            if (views!=null){
                score+=views;
            }
            if (likes!=null){
                score+=likes*3;
            }
            if (comment!=null){
                score+=comment*5;
            }
            if (collection!=null){
                score+=collection*8;
            }
            HotArticleVo hotArticleVo = new HotArticleVo();
            hotArticleVo.setScore(score);
            BeanUtils.copyProperties(apArticle,hotArticleVo);
            objects.add(hotArticleVo);
        }
        return objects;
    }
}
